﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsPlatform.PermissionManagement.IPermissionManagement;
using SportsPlatform.Base;
using SportsPlatform.Consts;
using SportsPlatform.Query.PermissionManagement.DepartmentRole;
using SportsPlatform.Model.PermissionManagement;
using SportsPlatform.Query.PermissionManagement.BaseQuery;
using SportsPlatform.Dto.PermissionManagement.DepartmentRole;
using Abp.Application.Services;
using Microsoft.AspNetCore.Http;
using SportsPlatform.Global;
using Abp.Domain.Repositories;

namespace SportsPlatform.PermissionManagement.PermissionManagement
{
    /// <summary>
    /// 部门角色对应表
    /// </summary>
    [RemoteService(false)]  //隐藏自动生成的WebApi
    public class DepartmentRoleService :SportsPlatformAppServiceBase, IDepartmentRoleService
    {
        private readonly IRepository<DepartmentRole, Guid> _deproleRepository;
        public DepartmentRoleService(IRepository<DepartmentRole, Guid> deproleRepository, IHttpContextAccessor context) : base(context)
        {
            _deproleRepository = deproleRepository;
        }
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<BaseResultDto> AddDepartmentRole(AddDepartmentRoleQuery query)
        {
            BaseResultDto baseResultDto = new();
            try
            {
                var token = UldAdminSessionContext.GetCurrentUserInfo(_context);
                var deproleinfo = ObjectMapper.Map<DepartmentRole>(query);
                deproleinfo.CreateTime=DateTime.Now;
                deproleinfo.CreateUserId = token.Id;
                await _deproleRepository.InsertAsync(deproleinfo);
                baseResultDto.IsSuccess = true;
                baseResultDto.Code = HttpResultCode.Success;
                baseResultDto.Message = "新增成功！";
            }
            catch (Exception e)
            {
                baseResultDto.IsSuccess = false;
                baseResultDto.Code = HttpResultCode.Error;
                baseResultDto.Message = e.Message.ToString();
            }
            return baseResultDto;
        }

        /// <summary>
        /// 根据Id删除信息
        /// </summary>
        /// <param name="deleteOrQueryModel"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BaseResultDto> DeleteDepartmentRole(DeleteOrGetModelQuery deleteOrQueryModel)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                //int ret = await _sugar.Updateable<DepartmentRole>()
                //    .Where(info => info.Id == deleteOrQueryModel.Id)
                //    .SetColumns(info => info.IsDeleted == true)
                //    .SetColumns(info => info.DeleteTime == DateTime.Now)
                //    .SetColumns(info => info.DeleteUserId == deleteOrQueryModel.DeleteUserId)
                //    .ExecuteCommandAsync();
                //basePage.Code = ret > 0 ? HttpResultCode.Success : HttpResultCode.SqlError;
            }
            catch (Exception e)
            {
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }

        /// <summary>
        /// 根据Id查询
        /// </summary>
        /// <param name="deleteOrQueryModel"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDto<GetDepartmentRoleDto>> GetDepartmentRole(DeleteOrGetModelQuery deleteOrQueryModel)
        {
            ResultDto<GetDepartmentRoleDto> pageData = new ResultDto<GetDepartmentRoleDto>();
            pageData.Code = HttpResultCode.SqlError;
            try
            {
                //DepartmentRole depaRole = await _sugar.Queryable<DepartmentRole>().SingleAsync(info => info.Id == deleteOrQueryModel.Id);
                //if (depaRole != null)
                //{
                //    pageData.Code = HttpResultCode.Success;
                //    pageData.Data = _mapper.Map<GetDepartmentRoleDto>(depaRole);
                //}
            }
            catch (Exception e)
            {
                pageData.Code = HttpResultCode.Error;
                pageData.Message = e.Message.ToString();
            }
            return pageData;
        }

        /// <summary>
        /// 获取信息List
        /// </summary>
        /// <param name="getDepartmentRoleQuery"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultListDto<GetDepartmentRoleDto>> GetDepartmentRoleList(GetDepartmentRoleQuery getDepartmentRoleQuery)
        {
            ResultListDto<GetDepartmentRoleDto> resultListDto = new ResultListDto<GetDepartmentRoleDto>();
            resultListDto.Code = HttpResultCode.SqlError;
            try
            {
                //var deparolelist = await _sugar.Queryable<DepartmentRole>()
                //        .WhereIF(getDepartmentRoleQuery.Id != null, info => info.Id == getDepartmentRoleQuery.Id)
                //        .WhereIF(getDepartmentRoleQuery.DepartmentId != null, info => info.DepartmentId == getDepartmentRoleQuery.DepartmentId)
                //        .WhereIF(getDepartmentRoleQuery.RoleId != null, info => info.RoleId == getDepartmentRoleQuery.RoleId)
                //       .WhereIF(getDepartmentRoleQuery.States != null, info => info.States == getDepartmentRoleQuery.States)
                //        .OrderByDescending(info => info.CreateTime)
                //        .ToListAsync();
                //if (deparolelist != null && deparolelist.Count > 0)
                //{
                //    resultListDto.Code = HttpResultCode.Success;
                //    resultListDto.Count = deparolelist.Count;
                //    List<GetDepartmentRoleDto> getDepartmentRoleDtos = new List<GetDepartmentRoleDto>();
                //    deparolelist.ForEach(info =>
                //    {
                //        getDepartmentRoleDtos.Add(_mapper.Map<GetDepartmentRoleDto>(info));
                //    });
                //    resultListDto.Data = getDepartmentRoleDtos;
                //}
            }
            catch (Exception e)
            {
                resultListDto.Code = HttpResultCode.Error;
                resultListDto.Message = e.Message.ToString();
            }
            return resultListDto;
        }

        /// <summary>
        /// 是否启用
        /// </summary>
        /// <param name="isStatesModel"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BaseResultDto> IsStatesDepartmentRole(IsStatesdModel isStatesModel)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
               
            }
            catch (Exception e)
            {
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }
        /// <summary>
        /// 跟新信息
        /// </summary>
        /// <param name="updateDepartmentRoleQuery"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BaseResultDto> UpdateDepartmentRole(UpdateDepartmentRoleQuery updateDepartmentRoleQuery)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
               
            }
            catch (Exception e)
            {
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }
    }
}
